package com.wuchao.jdbc.service.serviceimpl;

import com.wuchao.jdbc.basedao.BaseDaoOracleProperties;
import com.wuchao.jdbc.entity.T169;
import com.wuchao.jdbc.entity.XY;
import com.wuchao.jdbc.service.OracleService;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wuchao66
 * @Date 2023/9/13 15:32
 * @PackageName:com.wuchao.jdbc.service.serviceimpl
 * @ClassName: OracleServiceImpl
 * @Description: TODO
 * @Param
 * @Version 1.0
 */
public class OracleServiceImpl extends BaseDaoOracleProperties implements OracleService {
    @Override
    public String findt163namebyid(int id) throws SQLException {
        //得到链接对象
        Connection connection=getConnection();
        //sql
        String sql="{ call sys.findt163namebyid(?,?)}";
        //precall
        CallableStatement callableStatement = connection.prepareCall(sql);
        //注册参数
        callableStatement.setInt(1,id);
        callableStatement.registerOutParameter(2, Types.VARCHAR);
        //执行
        callableStatement.execute();
        return callableStatement.getString(2);
    }

    @Override
    public List<XY> findxyById1(int id) throws SQLException {
        Connection connection=getConnection();
        String sql="{call sys.findxyById1(?,?,?,?)}";
        CallableStatement callableStatement = connection.prepareCall(sql);
        callableStatement.setInt(1,id);
        callableStatement.registerOutParameter(2,Types.INTEGER);
        callableStatement.registerOutParameter(3,Types.VARCHAR);
        callableStatement.registerOutParameter(4,Types.INTEGER);
        callableStatement.execute();
      int id1=  callableStatement.getInt(2);
      String name=  callableStatement.getString(3);
      int age=  callableStatement.getInt(4);
       XY xy= new XY();
       xy.setSid(id1);
       xy.setName(name);
       xy.setSage(age);
        ArrayList<XY> xies = new ArrayList<>();
        xies.add(xy);
        return xies;
    }

   @Override
    public List<T169> findT169ById(int id) throws SQLException {
        //获取数据库连接
        Connection connection=getConnection();
        //定义查询语句
        String sql="{call sys.findT169ById(?,?,?,?)}";
        //准备调用存储过程
        CallableStatement call =
                connection.prepareCall(sql);
        //设置参数
        call.setInt(1,id);
        call.registerOutParameter(2,Types.INTEGER);
        call.registerOutParameter(3,Types.VARCHAR);
        call.registerOutParameter(4,Types.DATE);
        //执行查询
        call.execute();
        //创建T169对象
        T169 t169 = new T169();
        //设置T169对象的属性
        t169.setSid(call.getInt(2));
        t169.setSname(call.getString(3));
        t169.setStime(call.getDate(4));
        //创建T169列表
        List<T169> t169s = new ArrayList<>();
        //将T169对象添加到列表中
        t169s.add(t169);
        //返回T169列表
        return t169s;
    }
}
